package com.shenmazong.servertalkadmin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shenmazong.pojo.TbSubject;
import com.shenmazong.servertalkadmin.mapper.TbSubjectMapper;
import com.shenmazong.servertalkadmin.service.TbSubjectService;
import com.shenmazong.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class TbSubjectServiceImpl implements TbSubjectService {

    @Autowired
    TbSubjectMapper tbSubjectMapper;

    @Override
    public ResponseResult addSubjectUser(String subjectName) {
        ResponseResult result = ResponseResult.SUCCESS();
        HashMap<String, Object> resultMap = new HashMap<>();

        //--1 判断是否存在
        TbSubject subject = tbSubjectMapper.selectOne(new QueryWrapper<TbSubject>()
                .eq("subject_name", subjectName)
                .last("limit 1"));
        if(subject != null) {
            result.setSuccess(false);
            result.setCode(-1);
            result.setMessage("科目已存在");
            return result;
        }

        //--2 添加科目
        subject = new TbSubject();
        subject.setSubjectName(subjectName);

        tbSubjectMapper.insert(subject);
        if(subject.getId() != null) {
            resultMap.put("subject", subject);
            result.setResult(resultMap);
            return result;
        }

        //-- 增加失败
        result.setSuccess(false);
        result.setCode(-1);
        result.setMessage("科目增加失败");
        return result;
    }

    @Override
    public ResponseResult delSubjectUser(Integer subjectId) {
        ResponseResult result = ResponseResult.SUCCESS();
        HashMap<String, Object> resultMap = new HashMap<>();

        //--1 查询科目是否存在
        TbSubject subject = tbSubjectMapper.selectById(subjectId);
        if(subject == null) {
            result.setSuccess(false);
            result.setCode(-1);
            result.setMessage("科目不存在");
            return result;
        }

        //--2 查询这个科目是否已被删除
        if(subject.getStatus().equals(1)) {
            result.setFailMessage("科目已经删除");
            return result;
        }

        //--3 进行删除
        subject.setStatus(1);
        tbSubjectMapper.updateById(subject);

        resultMap.put("subject", subject);
        result.setResult(resultMap);

        return result;
    }

    @Override
    public ResponseResult setSubjectInfo(Integer subjectId, String subjectName) {
        ResponseResult result = ResponseResult.SUCCESS();
        HashMap<String, Object> resultMap = new HashMap<>();

        //--1 查询科目是否存在
        TbSubject subject = tbSubjectMapper.selectById(subjectId);
        if(subject == null) {
            result.setSuccess(false);
            result.setCode(-1);
            result.setMessage("科目不存在");
            return result;
        }

        //--2 查询这个科目是否已被删除
        if(subject.getStatus().equals(1)) {
            result.setFailMessage("科目已经删除");
            return result;
        }

        //--3 进行修改
        subject.setId(subjectId);
        subject.setSubjectName(subjectName);
        tbSubjectMapper.updateById(subject);

        resultMap.put("subject", subject);
        result.setResult(resultMap);

        return result;
    }

    @Override
    public ResponseResult getSubjectInfo(Integer subjectId) {
        ResponseResult result = ResponseResult.SUCCESS();
        HashMap<String, Object> resultMap = new HashMap<>();

        //--1 查询科目是否存在
        TbSubject subject = tbSubjectMapper.selectOne(new QueryWrapper<TbSubject>()
        .eq("id", subjectId).eq("status", 0).last("limit 1"));
        if(subject == null) {
            result.setFailMessage("科目不存在");
            return result;
        }

        resultMap.put("subject", subject);
        result.setResult(resultMap);

        return result;
    }

    @Override
    public ResponseResult getSubjectAll() {
        ResponseResult result = ResponseResult.SUCCESS();
        HashMap<String, Object> resultMap = new HashMap<>();

        //--1 查询科目是否存在
        List<TbSubject> subjects = tbSubjectMapper.selectList(
                new QueryWrapper<TbSubject>()
                        .eq("status", 0)
                        .orderByAsc("id")
        );
        if(subjects == null || subjects.size() <= 0) {
            result.setFailMessage("科目不存在");
            return result;
        }

        resultMap.put("subjects", subjects);
        result.setResult(resultMap);

        return result;
    }
}
